import 'package:flutter/material.dart';
import 'package:flutter_elf/widgets/loading/load_empty.dart';
import 'package:flutter_elf/widgets/loading/load_error.dart';
import 'package:flutter_elf/widgets/loading/loading_widget.dart';
final RouteObserver<PageRoute> myRouteObserver = RouteObserver<PageRoute>();

abstract class BasePageState<T extends StatefulWidget> extends State with WidgetsBindingObserver, RouteAware {
  String tagInStateWithLifecycle = 'StateWithLifecycle';
  ViewState viewState = ViewState.idle;
  T get widget => super.widget;

  bool _isVisibleToUser = false; // 是否对用户可见，表明在onResume-onPause调用过程中。也用来表示当前页面是否在栈顶。

  bool _systemDispatched = false;

  bool _didPush = false; // 表示是否调用了didPush方法。第一次进入会调用
  bool _didPopNext = false; // 表示是否调用了didPopNext方法。进入别的页面，再次返回时会调用。
  bool _didPop = false; // 表示是否调用了didPop方法。从Navigator中pop时会调用。

  int _didPopNextCount = 0;

  BuildContext mContext;
  @override
  void initState() {
    super.initState();
    mContext = context;
    _onMockCreate();
    _onMockResume();
    WidgetsBinding.instance.addObserver(this);
  }


  @override
  void deactivate() {
    super.deactivate();
    if (_didPop || _didPush || _didPopNextCount == 1) {
      _onMockPause();
    } else if (_didPopNext) {
      _onMockResume();
    }
    _resetValues();
  }

  void _resetValues() {
    _didPop = false;
    _didPopNext = false;
    _didPush = false;
    _didPopNextCount--;
  }

  @override
  void dispose() {
    _onMockDestroy();
    WidgetsBinding.instance.removeObserver(this);
    myRouteObserver.unsubscribe(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        _onMockResume();
        _systemDispatched = false;
        break;
      case AppLifecycleState.inactive:
        break;
      case AppLifecycleState.paused:
        _systemDispatched = true;
        _onMockPause();
        break;
      default:
        break;
    }
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    myRouteObserver.subscribe(this, ModalRoute.of(context));
  }

  @override
  void didPush() {
    _didPush = true;
  }

  @override
  void didPopNext() {
    _didPopNext = true;
    _didPopNextCount = 2;
  }

  @override
  void didPop() {
    _didPop = true;
  }

  void _onMockCreate() {
    onCreate();
  }

  @mustCallSuper
  void _onMockResume() {
    if (_systemDispatched && _isVisibleToUser || !_systemDispatched && !_isVisibleToUser) {
      _isVisibleToUser = true;
      onResume();
    }
  }

  @mustCallSuper
  void _onMockPause() {
    if (!_isVisibleToUser) {
      return;
    }
    if (!_systemDispatched) {
      _isVisibleToUser = false;
    }
    onPause();
  }
  void _onMockDestroy() {
    onDestroy();
  }

  void onCreate() {

  }

  void onResume() {

  }

  void onPause() {

  }

  void onDestroy() {

  }

  buildContent(Widget content){
    Widget widget;
    switch(viewState){
      case ViewState.error:
        widget = LoadError();
        break;
      case ViewState.idle:
        widget = LoadEmpty();
        break;
      case ViewState.busy:
        widget = LoadingWidget();
        break;
      case ViewState.success:
        widget = content;
        break;
    }
    return widget;
  }

}

enum ViewState { idle, busy, success, error }